package com.zf.yichat.api.controller.message;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zf.yichat.api.controller.BaseController;
import com.zf.yichat.api.dto.request.UltragroupRequest;
import com.zf.yichat.api.dto.request.UltragroupUserRequest;
import com.zf.yichat.dto.UltragroupUserDto;
import com.zf.yichat.dto.UserRemoveUltragroupDto;
import com.zf.yichat.mapper.MessageUltragroupMapper;
import com.zf.yichat.mapper.UserJoinUltragroupApplyMapper;
import com.zf.yichat.mapper.UserJoinUltragroupMapper;
import com.zf.yichat.mapper.UserRemoveUltragroupMapper;
import com.zf.yichat.model.*;
import com.zf.yichat.service.UserService;
import com.zf.yichat.service.config.RongCloudApplication;
import com.zf.yichat.service.config.UltragroupService;
import com.zf.yichat.utils.common.N3d;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 超级群API入口
 */
@RestController
@RequestMapping("ultragroup")
@Slf4j
public class MessageUltragroupController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private UltragroupService ultragroupService;

    @Resource
    private MessageUltragroupMapper messageUltragroupMapper;

    @Resource
    private UserRemoveUltragroupMapper userRemoveUltragroupMapper;

    @Resource
    private UserJoinUltragroupMapper userJoinUltragroupMapper;

    @Resource
    private UserJoinUltragroupApplyMapper userJoinUltragroupApplyMapper;

    @Autowired
    private RongCloudApplication rongCloudApplication;


    /**
     * 用户申请进入超级群
     * @return
     */
    @RequestMapping("/userApplyForUltragroup")
    @ResponseBody
    public FsResponse userApplyForUltragroup(@RequestBody UltragroupRequest request) {

        try {

            // 首先查询该用户是否已申请过
            Long userId = getUserId();
            if (null == userId) {
                return FsResponseGen.failMsg("参数错误[userID]");
            }

            if (!userService.realValidStatus(userId)) {
                return FsResponseGen.failMsg("请先实名认证");
            }

            List<UserBank> userBanks = userService.selectBankList(userId);
            if (null == userBanks || userBanks.size() == 0) {
                return FsResponseGen.failMsg("请先绑定银行卡");
            }

            String ultragroupId = request.getUltragroupId();
            Long decodeUltragroupId = N3d.decode(ultragroupId);
            String encodeUltragroup = Long.toString(decodeUltragroupId);

            // 查询该用户是否被踢出群
            UserRemoveUltragroup removeUltragroup = userRemoveUltragroupMapper.queryRemoveUser(userId, encodeUltragroup);
            if (null != removeUltragroup) {
                return FsResponseGen.successMsg("拒绝申请加入！");
            }

            // 查询超级群是否需要审核
            MessageUltragroup messageUltragroup = messageUltragroupMapper.selectOneByUltragroupId(ultragroupId);
            if (null != messageUltragroup) {
                String approval = messageUltragroup.getApproval();
                if (MessageUltragroup.APPROVAL_0.equals(approval)) {
                    // 需要审核
                    UserJoinUltragroupApply user = userJoinUltragroupApplyMapper.queryApplyForUltragroup(userId, encodeUltragroup);
                    if (null == user) {
                        // 新增申请记录
                        UserJoinUltragroupApply joinUltragroup = new UserJoinUltragroupApply();
                        joinUltragroup.setUserId(userId);
                        joinUltragroup.setUltragroupId(encodeUltragroup);
                        joinUltragroup.setCreateTime(new Date());
                        joinUltragroup.setStatus(UserJoinChatroom.STATUS_AUDIT);
                        userJoinUltragroupApplyMapper.insert(joinUltragroup);
                        return FsResponseGen.successMsg("申请成功，请等待审核结果！");

                    }

                    // 用户审核状态
                    String status = user.getStatus();
                    if (UserJoinUltragroup.STATUS_APPROVED.equals(status)) {

                        // 查询是否已有超级群成员记录, 如果有申请成功记录没有成员记录直接更新申请状态为待审核（针对删除成员后重新申请的情况）
                        UserJoinUltragroup ultragroup = userJoinUltragroupMapper.queryUserInUltragroup(userId, encodeUltragroup);
                        if (null == ultragroup) {
                            UserJoinUltragroupApply ultragroupApply = new UserJoinUltragroupApply();
                            ultragroupApply.setId(user.getId());
                            ultragroupApply.setStatus(UserJoinChatroom.STATUS_AUDIT);
                            userJoinUltragroupApplyMapper.updateByPrimaryKeySelective(ultragroupApply);
                            return FsResponseGen.successMsg("申请成功，请等待审核结果！");
                        }

                        return FsResponseGen.successMsg("申请成功！");
                    }
                    if (UserJoinChatroom.STATUS_AUDIT.equals(status)) {
                        return FsResponseGen.successMsg("审核中！");
                    }
                    if (UserJoinChatroom.STATUS_REJECTION.equals(status)) {
                        UserJoinUltragroupApply ultragroupApply = new UserJoinUltragroupApply();
                        ultragroupApply.setId(user.getId());
                        ultragroupApply.setStatus(UserJoinChatroom.STATUS_AUDIT);
                        userJoinUltragroupApplyMapper.updateByPrimaryKeySelective(ultragroupApply);
                        return FsResponseGen.successMsg("申请成功，请等待审核结果！");
                    }
                    if (UserJoinChatroom.STATUS_REJECTED.equals(status)) {
                        return FsResponseGen.successMsg("拒绝申请加入！");
                    }

                }else {
                    // 不需要审核，直接审批通过且加入群组成员列表中
                    // 新增申请记录
                    UserJoinUltragroupApply ultragroupApply = new UserJoinUltragroupApply();
                    ultragroupApply.setUserId(userId);
                    ultragroupApply.setUltragroupId(encodeUltragroup);
                    ultragroupApply.setCreateTime(new Date());
                    ultragroupApply.setStatus(UserJoinChatroom.STATUS_APPROVED);
                    userJoinUltragroupApplyMapper.insert(ultragroupApply);

                    // 新增群组成员
                    // 增加群成员信息
                    if (ultragroupService.joinUltragroup(encodeUltragroup, Long.toString(userId))) {
                        UserJoinUltragroup joinUltragroup = new UserJoinUltragroup();
                        joinUltragroup.setUserId(userId);
                        joinUltragroup.setUltragroupId(encodeUltragroup);
                        joinUltragroup.setLevel(UserJoinUltragroup.LEVEL_MEMBER);
                        joinUltragroup.setCreateTime(new Date());
                        return FsResponseGen.successData(userJoinUltragroupMapper.insertSelective(joinUltragroup));
                    }
                }
            }



            return FsResponseGen.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FsResponseGen.gen(false);
    }

    /**
     * 查询超级群
     * @return
     */
    @RequestMapping("/queryAllUltragroup")
    @ResponseBody
    public FsResponse queryAllUltragroup() {

        try {
            List<MessageUltragroup> list = ultragroupService.getUltragroupList();
            return FsResponseGen.successData(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FsResponseGen.gen(false);
    }

    /**
     * 用户已加入的超级群列表
     * @return
     */
    @RequestMapping("/queryJoinUltragroup")
    @ResponseBody
    public FsResponse queryJoinUltragroup() {

        try {
            Long userId = getUserId();
            if (null == userId) {
                return FsResponseGen.failMsg("参数错误[userID]");
            }

            List<MessageUltragroup> list = ultragroupService.queryJoinUltragroup(userId);
            return FsResponseGen.successData(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FsResponseGen.gen(false);
    }

    /**
     * 超级群踢出成员
     * @return
     */
    @RequestMapping("/removedUser")
    @ResponseBody
    public FsResponse removedUser(@RequestBody UltragroupRequest request) {

        String userId = request.getUserId();
        if (StringUtils.isBlank(userId)) {
            return FsResponseGen.failMsg("参数错误[userID]");
        }

        try {

            if (!groupManagement(request.getUltragroupId())) {
                return FsResponseGen.failMsg("暂无操作权限！");
            }

            HttpPost httpPost = rongCloudApplication.imConstructor("ultragroup/quit");

            // 设置请求消息体参数
            String ultragroupId = request.getUltragroupId();
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("groupId", ultragroupId));
            params.add(new BasicNameValuePair("userId", userId));
            httpPost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));

            String response = rongCloudApplication.executeReq(httpPost);
            JSONObject jsonObject = JSON.parseObject(response);
            String code = jsonObject.getString("code");

            if ("200".equals(code)) {
                Long decodeUltragroupId = N3d.decode(ultragroupId);
                Long decodeUserId = N3d.decode(userId);
                // 退出成功后需要将本地数据删除
                userJoinUltragroupMapper.deleteUltragroupMember(Long.toString(decodeUltragroupId), Long.toString(decodeUserId));

                // 记录被踢出的用户
                UserRemoveUltragroup removeUltragroup = new UserRemoveUltragroup();
                removeUltragroup.setUserId(decodeUserId);
                removeUltragroup.setUltragroupId(Long.toString(decodeUltragroupId));
                removeUltragroup.setCreateTime(new Date());
                userRemoveUltragroupMapper.insert(removeUltragroup);


                return FsResponseGen.successData(response);
            }else {
                String errorMessage = jsonObject.getString("errorMessage");
                return FsResponseGen.failMsg(errorMessage);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FsResponseGen.gen(false);
    }

    /**
     * 退出超级群
     * @return
     */
    @RequestMapping("/quit")
    @ResponseBody
    public FsResponse quitGroup(@RequestBody UltragroupRequest request) {
        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误[userID]");
        }

        if (!userService.realValidStatus(userId)) {
            return FsResponseGen.failMsg("请先实名认证");
        }

        try {
            HttpPost httpPost = rongCloudApplication.imConstructor("ultragroup/quit");

            // 设置请求消息体参数
            String ultragroupId = request.getUltragroupId();
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("groupId", ultragroupId));
            params.add(new BasicNameValuePair("userId", N3d.encode(userId)));
            httpPost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));

            String response = rongCloudApplication.executeReq(httpPost);
            JSONObject jsonObject = JSON.parseObject(response);
            String code = jsonObject.getString("code");

            if ("200".equals(code)) {
                Long decodeUltragroupId = N3d.decode(ultragroupId);
                // 退出成功后需要将本地数据删除
                userJoinUltragroupMapper.deleteUltragroupMember(Long.toString(decodeUltragroupId), Long.toString(userId));
                return FsResponseGen.successData(response);
            }else {
                String errorMessage = jsonObject.getString("errorMessage");
                return FsResponseGen.failMsg(errorMessage);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FsResponseGen.gen(false);
    }

    /**
     * 单人禁言-添加禁言成员
     * @return
     */
    @RequestMapping("/userbanned/add")
    @ResponseBody
    public FsResponse banAdd(@RequestBody UltragroupRequest request) {


        try {
            if (!groupManagement(request.getUltragroupId())) {
                return FsResponseGen.failMsg("暂无操作权限！");
            }
            HttpPost httpPost = rongCloudApplication.imConstructor("ultragroup/userbanned/add");

            // 设置请求消息体参数
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("groupId", request.getUltragroupId()));
            params.add(new BasicNameValuePair("userIds", request.getUserId()));
            httpPost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));

            String response = rongCloudApplication.executeReq(httpPost);
            return rongCloudApplication.judgmentResults(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FsResponseGen.gen(false);
    }

    /**
     * 单人禁言-移除禁言成员
     * @return
     */
    @RequestMapping("/userbanned/del")
    @ResponseBody
    public FsResponse banDel(@RequestBody UltragroupRequest request) {

        try {
            HttpPost httpPost = rongCloudApplication.imConstructor("ultragroup/userbanned/del");

            // 设置请求消息体参数
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("groupId", request.getUltragroupId()));
            params.add(new BasicNameValuePair("userIds", request.getUserId()));
            httpPost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));

            String response = rongCloudApplication.executeReq(httpPost);
            return rongCloudApplication.judgmentResults(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FsResponseGen.gen(false);
    }

    /**
     * 单人禁言-获取禁言成员
     * @return
     */
    @RequestMapping("/userbanned/get")
    @ResponseBody
    public FsResponse banGet(@RequestBody UltragroupRequest request) {

        try {
            HttpPost httpPost = rongCloudApplication.imConstructor("ultragroup/userbanned/get");

            // 设置请求消息体参数
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("groupId", request.getUltragroupId()));
            httpPost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));

            String response = rongCloudApplication.executeReq(httpPost);

            JSONObject jsonObject = JSON.parseObject(response);
            String code = jsonObject.getString("code");

            if ("200".equals(code)) {
                String users = jsonObject.getString("users");
                List<UltragroupUserDto> list = JSON.parseArray(users, UltragroupUserDto.class);

                ArrayList<UltragroupUserDto> chatroomUserDtos = new ArrayList<>();
                for (UltragroupUserDto dto : list){

                    UltragroupUserDto userDto = new UltragroupUserDto();
                    userDto.setId(dto.getId());

                    // 加密的用户id
                    String userId = dto.getId();
                    Long decodeUserId = N3d.decode(userId);
                    User user = userService.selectById(decodeUserId);
                    if (null != user) {
                        String nick = user.getNick();
                        String avatar = user.getAvatar();
                        userDto.setAvatar(avatar);
                        userDto.setNick(nick);
                    }

                    chatroomUserDtos.add(userDto);
                }

                return FsResponseGen.successData(chatroomUserDtos);
            }


            return rongCloudApplication.judgmentResults(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FsResponseGen.gen(false);
    }

    /**
     * 全体禁言-设置超级群禁言状态
     * @return
     */
    @RequestMapping("/globalbanned/set")
    @ResponseBody
    public FsResponse setGlobalBanned(@RequestBody UltragroupRequest request) {

        try {
            if (!groupManagement(request.getUltragroupId())) {
                return FsResponseGen.failMsg("暂无操作权限！");
            }

            HttpPost httpPost = rongCloudApplication.imConstructor("ultragroup/globalbanned/set");

            // 设置请求消息体参数
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("groupId", request.getUltragroupId()));
            params.add(new BasicNameValuePair("status", request.getStatus()));
            httpPost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));

            String response = rongCloudApplication.executeReq(httpPost);
            return rongCloudApplication.judgmentResults(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FsResponseGen.gen(false);
    }

    /**
     * 全体禁言-查询超级群禁言状态
     * @return
     */
    @RequestMapping("/globalbanned/get")
    @ResponseBody
    public FsResponse getGlobalBanned(@RequestBody UltragroupRequest request) {

        try {
            HttpPost httpPost = rongCloudApplication.imConstructor("ultragroup/globalbanned/get");

            // 设置请求消息体参数
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("groupId", request.getUltragroupId()));
            httpPost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));

            String response = rongCloudApplication.executeReq(httpPost);
            return rongCloudApplication.judgmentResults(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FsResponseGen.gen(false);
    }

    /**
     * 全体禁言-添加禁言白名单
     * @return
     */
    @RequestMapping("/banned/whitelist/add")
    @ResponseBody
    public FsResponse addWhitelist(@RequestBody UltragroupRequest request) {

        try {
            HttpPost httpPost = rongCloudApplication.imConstructor("ultragroup/banned/whitelist/add");

            // 设置请求消息体参数
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("groupId", request.getUltragroupId()));
            params.add(new BasicNameValuePair("userIds", request.getUserId()));
            httpPost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));

            String response = rongCloudApplication.executeReq(httpPost);
            return rongCloudApplication.judgmentResults(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FsResponseGen.gen(false);
    }

    /**
     * 全体禁言-移除禁言白名单
     * @return
     */
    @RequestMapping("/banned/whitelist/del")
    @ResponseBody
    public FsResponse delWhitelist(@RequestBody UltragroupRequest request) {

        try {
            if (!groupManagement(request.getUltragroupId())) {
                return FsResponseGen.failMsg("暂无操作权限！");
            }

            HttpPost httpPost = rongCloudApplication.imConstructor("ultragroup/banned/whitelist/del");

            // 设置请求消息体参数
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("groupId", request.getUltragroupId()));
            params.add(new BasicNameValuePair("userIds", request.getUserId()));
            httpPost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));

            String response = rongCloudApplication.executeReq(httpPost);
            return rongCloudApplication.judgmentResults(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FsResponseGen.gen(false);
    }

    /**
     * 全体禁言-获取禁言白名单
     * @return
     */
    @RequestMapping("/banned/whitelist/get")
    @ResponseBody
    public FsResponse getWhitelist(@RequestBody UltragroupRequest request) {

        try {
            HttpPost httpPost = rongCloudApplication.imConstructor("ultragroup/banned/whitelist/get");

            // 设置请求消息体参数
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("groupId", request.getUltragroupId()));
            httpPost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));

            String response = rongCloudApplication.executeReq(httpPost);
            return rongCloudApplication.judgmentResults(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FsResponseGen.gen(false);
    }

    /**
     * 根据群ID获取被踢的人员列表
     * @return
     */
    @RequestMapping("/queryRemovedUserByUltrgroup")
    @ResponseBody
    public FsResponse queryRemovedUserByUltrgroup(@RequestBody UltragroupRequest request) {

        try {
            String ultragroupId = request.getUltragroupId();
            if (StringUtils.isBlank(ultragroupId)) {
                return FsResponseGen.failMsg("超级群ID参数错误！");
            }

            Long decodeUltraGroupId = N3d.decode(ultragroupId);

            List<UserRemoveUltragroupDto> list = userRemoveUltragroupMapper.queryRemoveUserList(String.valueOf(decodeUltraGroupId));
            list.stream().forEach(dto -> {
                try {
                    Long userId = dto.getUserId();
                    String encodeUserId = N3d.encode(userId);
                    dto.setId(encodeUserId);

                    Long ultraGroupId = dto.getUltraGroupId();
                    String encodeUltraGroupId = N3d.encode(ultraGroupId);
                    dto.setUltragroupId(encodeUltraGroupId);

                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("N3d加密错误！");
                }
            });
            return FsResponseGen.successData(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FsResponseGen.gen(false);
    }

    /**
     * 移除超级群被踢的人员
     * @return
     */
    @RequestMapping("/removedKickedUser")
    @ResponseBody
    public FsResponse removedKickedUser(@RequestBody UltragroupRequest request) {

        try {
            String ultragroupId = request.getUltragroupId();
            if (StringUtils.isBlank(ultragroupId)) {
                return FsResponseGen.failMsg("超级群ID参数错误！");
            }

            String userId = request.getUserId();
            if (StringUtils.isBlank(userId)) {
                return FsResponseGen.failMsg("用户ID参数错误！");
            }

            Long decodeUserId = N3d.decode(userId);
            Long decodeUltraGroupId = N3d.decode(ultragroupId);


            return FsResponseGen.successData(userRemoveUltragroupMapper.removedKickedUser(decodeUserId, decodeUltraGroupId));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FsResponseGen.gen(false);
    }



    /**
     * 模糊查询超级群的成员
     * @return
     */
    @RequestMapping("/queryUserUltragroup")
    @ResponseBody
    public FsResponse queryUserUltragroup(@RequestBody UltragroupUserRequest request) {

        try {

            // 模糊查询超级群内成员
            String ultragroupId = request.getUltragroupId();
            if (StringUtils.isBlank(ultragroupId)) {
                return FsResponseGen.failMsg("参数错误，超级群ID不存在！");
            }
            // 查询超级群是否存在
            MessageUltragroup messageUltragroup = messageUltragroupMapper.selectOneByUltragroupId(ultragroupId);
            if (null == messageUltragroup) {
                return FsResponseGen.failMsg("超级群不存在！");
            }

            String appid = request.getAppid();
            String nick = request.getNick();
            Long decodeChatroomId = N3d.decode(ultragroupId);

            List<User> list = userJoinUltragroupMapper.queryUserByParam(Long.toString(decodeChatroomId), appid, nick);
            return FsResponseGen.successData(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FsResponseGen.gen(false);
    }


    /**
     * 查询用户是否在该超级群
     * @return
     */
    @RequestMapping("/queryUserInUltragroup")
    @ResponseBody
    public FsResponse queryUserInUltragroup(@RequestBody UltragroupUserRequest request) {

        try {
            Long userId = getUserId();
            String ultragroupId = request.getUltragroupId();
            Long decodeUltragroupId = N3d.decode(ultragroupId);

            UserJoinUltragroup user = userJoinUltragroupMapper.queryUserInUltragroup(userId, Long.toString(decodeUltragroupId));
            if (null == user) {
                return FsResponseGen.successData(false);
            }
            return FsResponseGen.successData(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FsResponseGen.gen(false);
    }

    /**
     * 查询操作者是否是群管
     * @param ultragroupId
     * @return
     */
    private boolean groupManagement(String ultragroupId) throws Exception {
        Long userId = getUserId();

        Long decodeUltragroupId = N3d.decode(ultragroupId);
        UserJoinUltragroup user = userJoinUltragroupMapper.queryUserInUltragroup(userId, Long.toString(decodeUltragroupId));

        if (null == user) {
            return false;
        }

        String level = user.getLevel();
        if (StringUtils.isBlank(level)) {
            return false;
        }

        if (UserJoinUltragroup.LEVEL_MEMBER.equals(level)) {
            return false;
        }
        return true;
    }

}
